package com.itheima.reggie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.Pojo.Category;
import com.itheima.reggie.Service.CategoryService;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")

/**
 * 分类管理
 */
public class CategoryController {

    @Autowired
    private CategoryService service;

    /**
     * 新增分类管理
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
        service.save(category);
        return R.success("新增分类成功");

    }


    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){

        Page<Category> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        service.page(page1,queryWrapper);
        return R.success(page1);

    }

    @DeleteMapping
    public R<String> delete(Long ids){

        service.removeid(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> updateid(@RequestBody Category category){

        log.info("category  :{}",category);
        service.updateById(category);
        return R.success("修改成功");

    }

    /**
     * 根据条件查询,不加条件的话就是查询所有的分类信息，是分类的信息，不是具体菜品或套餐的信息
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> dishcategory(Category category){
        LambdaQueryWrapper<Category> lambdaQuery = new LambdaQueryWrapper<>();
//      根据分类条件进行匹配，看是菜品分类还是套餐分类
        lambdaQuery.eq(category.getType()!=null,Category::getType,category.getType());
//      根据Sort属性升序排序，Sort相同时根据UpdateTime属性降序排序
        lambdaQuery.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
//      条件查询
        List<Category> list = service.list(lambdaQuery);

        return R.success(list);


    }


}
